﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PankowGame13
{
    public enum ItemTypes
    {
        Helm,
        Chest,
        Legs,
        Boots,
        Weapon,
        Shield,
        Ring,
        Necklace,
        Potion,
        Treasure,
        Empty
    }
    class Item
    {
        public string itemName { get; set; }
        public ItemTypes itemType { get; set; }
        public int[] itemBuffs { get; set; }
        public string itemTexture { get; set; }
        public Texture2D itemPicture { get; set; }
        public int treasureAmount { get; set; }
        public List<string> helms { get; set; }
        public List<string> armors { get; set; }
        public List<string> legs { get; set; }
        public List<string> boots { get; set; }
        public List<string> weapons { get; set; }
        public List<string> shields { get; set; }
        public List<string> rings { get; set; }
        public List<string> necklaces { get; set; }
        public int itemLevel { get; set; }
        public bool isMerchantItem { get; set; }
        
        /* Potions - 40
         * Weapons - 20
         * Armor - 20
         * Jewelry - 15
         * Treasure - 5
         * */
        public Item(int level, ContentManager content, int treasureFind, bool merchant,
            List<string> helmItems, List<string> armorItems, List<string> legItems,
            List<string> bootItems, List<string> weaponItems, List<string> shieldItems,
            List<string> ringItems, List<string> necklaceItems)
        {
            isMerchantItem = merchant;
            itemLevel = level;
            itemBuffs = new int[8];
            helms = helmItems;
            armors = armorItems;
            legs = legItems;
            boots = bootItems;
            weapons = weaponItems;
            shields = shieldItems;
            rings = ringItems;
            necklaces = necklaceItems;

            Random rand = new Random();
            int chance = rand.Next(1, 101);

            if (level >= 20 && level < 30)
            {
                level = 20;
            }
            if (level >= 30 && level < 40)
            {
                level = 30;
            }
            //if (level >= 40 && level < 50)
            //{
            //    level = 40;
            //}
            //if (level >= 50 && level < 60)
            //{
            //    level = 50;
            //}
            //if (level >= 60)
            //{
            //    level = 60;
            //}
            //no items after 60.  suck it player1!


            if (chance >= 1 && chance <= 40)
            {
                //potion
                itemType = ItemTypes.Potion;
                itemTexture = "potion";
                itemName = "Health Potion";
                itemPicture = content.Load<Texture2D>(itemTexture);
                for (int i = 0; i < 8; i++)
                {
                    itemBuffs[i] = 0;
                }
            }
            if (chance > 40 && chance <= 60)
            {
                if (level <= 30) //represents the max by 10 level of items created
                {
                    //weapon
                    itemType = ItemTypes.Weapon;
                    itemTexture = "weapon";
                    itemPicture = content.Load<Texture2D>(itemTexture);

                    string[] itemStats = new string[11];

                    //ONLY PICK ITEM BY LEVEL!
                    bool found = false;
                    int randItem = 0;
                    while (!found)
                    {
                        randItem = rand.Next(0, weapons.Count());
                        string[] tempStats = new string[11];
                        tempStats = weapons[randItem].Split(',');
                        if (Convert.ToInt32(tempStats[2]) == level)
                        {
                            found = true;
                        }
                    }



                    itemStats = weapons[randItem].Split(',');

                    for (int i = 0; i < 8; i++)
                    {
                        itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                    }

                    itemName = itemStats[1];
                }
                else
                {
                    if (isMerchantItem)
                    {
                        itemType = ItemTypes.Empty;
                        itemName = "Nothing";
                        itemPicture = content.Load<Texture2D>(@"Loot Chest");
                    }
                    else
                    {
                        itemType = ItemTypes.Treasure;
                        treasureAmount = (rand.Next(1 + level, 5 + level) + level) * (2 + treasureFind);
                    }
                }
            }
            if (chance > 60 && chance <= 80)
            {
                //armor
                int armorChance = rand.Next(1, 101);
                if (level <= 30)
                {
                    if (armorChance <= 20)
                    {
                        itemType = ItemTypes.Helm;
                        itemTexture = "helmet";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = helms[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = helms[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }

                        itemName = itemStats[1];
                    }
                    if (armorChance > 20 && armorChance <= 40)
                    {
                        itemType = ItemTypes.Chest;
                        itemTexture = "chest";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = armors[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = armors[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }

                        itemName = itemStats[1];
                    }
                    if (armorChance > 40 && armorChance <= 60)
                    {
                        itemType = ItemTypes.Legs;
                        itemTexture = "legs";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = legs[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = legs[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }
                        itemName = itemStats[1];
                    }
                    if (armorChance > 60 && armorChance <= 80)
                    {
                        itemType = ItemTypes.Boots;
                        itemTexture = "boots";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = boots[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = boots[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }

                        itemName = itemStats[1];
                    }
                    if (armorChance > 80)
                    {
                        itemType = ItemTypes.Shield;
                        itemTexture = "shield";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = shields[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = shields[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }

                        itemName = itemStats[1];
                    }
                }
                else
                {
                    if (isMerchantItem)
                    {
                        itemType = ItemTypes.Empty;
                        itemName = "Nothing";
                        itemPicture = content.Load<Texture2D>(@"Loot Chest");
                    }
                    else
                    {
                        itemType = ItemTypes.Treasure;
                        treasureAmount = (rand.Next(1 + level, 5 + level) + level) * (2 + treasureFind);
                    }
                }
            }
            if (chance > 80 && chance <= 95)
            {
                //jewelry
                int jewelryChance = rand.Next(1, 101);
                if (level <= 30)
                {
                    if (jewelryChance <= 50)
                    {
                        itemType = ItemTypes.Ring;
                        itemTexture = "ring";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = rings[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = rings[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }

                        itemName = itemStats[1];
                    }
                    if (jewelryChance > 50)
                    {
                        itemType = ItemTypes.Necklace;
                        itemTexture = "necklace";
                        itemPicture = content.Load<Texture2D>(itemTexture);

                        string[] itemStats = new string[11];

                        bool found = false;
                        int randItem = 0;
                        while (!found)
                        {
                            randItem = rand.Next(0, weapons.Count());
                            string[] tempStats = new string[11];
                            tempStats = necklaces[randItem].Split(',');
                            if (Convert.ToInt32(tempStats[2]) == level)
                            {
                                found = true;
                            }
                        }
                        itemStats = necklaces[randItem].Split(',');

                        for (int i = 0; i < 8; i++)
                        {
                            itemBuffs[i] = Convert.ToInt32(itemStats[i + 3]);
                        }

                        itemName = itemStats[1];
                    }
                }
                else
                {
                    if (isMerchantItem)
                    {
                        itemType = ItemTypes.Empty;
                        itemName = "Nothing";
                        itemPicture = content.Load<Texture2D>(@"Loot Chest");
                    }
                    else
                    {
                        itemType = ItemTypes.Treasure;
                        treasureAmount = (rand.Next(1 + level, 5 + level) + level) * (2 + treasureFind);
                    }
                }
                
            }
            if (chance > 95)
            {
                if (isMerchantItem)
                {
                    itemType = ItemTypes.Empty;
                    itemName = "Nothing";
                    itemPicture = content.Load<Texture2D>(@"Loot Chest");
                }
                else
                {
                    itemType = ItemTypes.Treasure;
                    treasureAmount = (rand.Next(1 + level, 5 + level) + level) * (2 + treasureFind);
                }
            }
            if (itemType == ItemTypes.Treasure)
            {
                if (level > 5 && level < 10)
                {
                    treasureAmount *= 2;
                }
                if (level >= 10 && level < 20)
                {
                    treasureAmount *= 3;
                }
                if (level >= 20 && level < 30)
                {
                    treasureAmount *= 4;
                }
                if (level >= 30 && level < 40)
                {
                    treasureAmount *= 8;
                }
                if (level >= 40 && level < 50)
                {
                    treasureAmount *= 16;
                }
                if (level >= 50)
                {
                    treasureAmount *= 32;
                }
            }
        }
        public Item(ContentManager content)
        {
            //creates the empty items for start of game
            itemName = "Empty";
            itemType = ItemTypes.Empty;
            itemTexture = "Loot Chest";
            itemPicture = content.Load<Texture2D>(itemTexture);
            itemBuffs = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
        }
    }
}
